package com.gitee.sop.gateway.config;

import com.gitee.sop.gatewaycommon.bean.ApiConfig;
import com.gitee.sop.redis.service.RedisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author Admin
 */
@Component
public class MyConfig {

    public static String ACCESS_TOKEN_PREFIX = "com.gitee.sop.oauth2_access_token:";

    @Resource
    private RedisService redisService;

    @PostConstruct
    public void after() {
        ApiConfig.getInstance().setTokenValidator(apiParam -> {
            // 获取客户端传递过来的token
            String token = apiParam.fetchAccessToken();
            if (StringUtils.isNotBlank(token)) {
                String key = ACCESS_TOKEN_PREFIX + token;
                Object object = redisService.getCacheObject(key);
                if (Objects.nonNull(object)) {
                    return true;
                }
            }
            return false;
        });
    }
}